package com.linkcard.list.persistence;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.linkcard.commen.tools.Page;
import com.linkcard.list.model.Activities;

public class ActivitiesDaoImpl extends HibernateDaoSupport implements
		ActivitiesDao {

	//添加活动
	public boolean addActivities(Activities activities) {
		try
		{
			this.getHibernateTemplate().save(activities);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//活动列表
	public List activitesList(final Page pageObj,final String searchStr) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Activities activities,Category category,Business business where activities.categoryId=category.id and business.id=activities.businessId";
				if(!searchStr.equals(""))
				{
					String[] str=searchStr.split("_");
					if(!str[0].equals("-2"))
					{
					  hql+=" and activities.categoryId="+str[0];
					}
					if(!str[1].equals("-2"))
					{
					  hql+=" and activities.subCategoryId="+str[1];
					}
					if(!str[2].equals("-2"))
					{
					  hql+=" and activities.businessId="+str[2];
					}
					if(str.length==4)
					{
					hql+=" and (activities.title like '%"+str[3]+"%'";
					hql+=" or activities.description like '%"+str[3]+"%')";
					}
				}
				hql+=" order by activities.datetime desc ";
				int rowCount = getRowCount(hql);
				  Query query=session.createQuery(hql);
				  if(pageObj!=null)
				  {
				  query.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				  query.setMaxResults(pageObj.getPageSize());
		         pageObj.setRowCount(rowCount);
		         pageObj.setTotalPage(rowCount);
		         pageObj.setPreNextPage();
				  }
				return query.list();
			}
		});
		return list;
	}
	/**
	 * 获得记录数
	 */
	public int getRowCount(final String hql) {
		Integer rowCount = 0;
		rowCount = (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(hql).list().size();
					}
				});
		return rowCount;
	}

	//查找活动
	public Activities findActivities(Integer id) {
		
		Activities activities=(Activities)this.getHibernateTemplate().get(Activities.class, id);
		return activities;
	}

	//修改活动
	public boolean findActivities(Activities activities) {
		try
		{
		Activities a=(Activities)this.getHibernateTemplate().get(Activities.class,activities.getId() );
		activities.setDatetime(a.getDatetime());
		BeanUtils.copyProperties(a, activities);
		this.getHibernateTemplate().update(a);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//删除活动
	public boolean deleteActivities(final int ids) {
		boolean isSuccess=(Boolean)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				try
				{
				String hql="delete from Activities activities where activities.id in("+ids+")";
				//hql="delete from Business business where business.id in(select business.id from Business business where business.id in("+ids+"))";
				Query query =session.createQuery(hql);
				query.executeUpdate();
				}catch(Exception e)
				{
					e.printStackTrace();
					return false;
				}
				return true;
			}
			
		});
		return isSuccess;
	}

	//获得单个　活动
	@SuppressWarnings("unchecked")
	public List singleActivities(final Integer id) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="from Activities activities,Category category,Business business where activities.categoryId=category.id and activities.businessId=business.id and activities.id="+id;
				Query query=session.createQuery(hql);
				return query.list();
			}
			
		});
		return list;
	}

}
